define(["jquery"],function($){
	var quest = {};

	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	if( obj.list && $.isArray(obj.list) && obj.list.length > 0 ){
    		for(var i = 0; i < obj.list.length; i++){
    			if( typeof obj.list[i] == "string" ){
    				var str = obj.list[i];
    				obj.list[i] = {};
    				obj.list[i].key = str;
    				obj.list[i].text = str;
    			}
    		}
    	}
    	if( obj.val && typeof obj.val != "string" ) obj.val = JSON.stringify( obj.val );
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_checkbox" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'		{{if status=="default"}}'+
				'   		<div class="control-input checkbox">'+
				'				{{each list as item i}}'+
				'					<label class="checkbox-inline">'+
				'						<input type="checkbox" class="validatebox" name="{{name}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}">'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{else if status=="readonly"}}'+	
				'   		<div class="control-input checkbox disabled">'+
				'				{{each list as item i}}'+
				'					<label class="checkbox-inline">'+
				'						<input type="checkbox" class="validatebox" name="{{name}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{else if status=="disabled"}}'+
				'   		<div class="control-input checkbox disabled">'+
				'				{{each list as item i}}'+
				'					<label class="checkbox-inline">'+
				'						<input type="checkbox" class="validatebox" name="{{name}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{/if}}'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			if( value.quesValue && value.quesValue.indexOf("[") >= 0 && value.quesValue.indexOf("]") >= 0 ) value.quesValue = JSON.parse(value.quesValue);
			if( value.quesValue && $.isArray(value.quesValue) && value.quesValue.length > 0 ){
				for (var i = 0; i < value.quesValue.length; i++) {
					$dom.each(function(j){
						if( value.quesValue[i] == $(this).val() ){
							$(this).attr("checked","checked");
							return true; // 退出本次循环
						}
					});
				}
			}
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var domArr = $dom.parents(".qaCard").find("input[type='checkbox'][name='" + name + "']:checked");
		var val = [];
		domArr.each(function(){
			val.push( $(this).val() );
		});
		var value = {};
		value.quesValue = JSON.stringify(val);
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}
    
	return quest;
});